//
//  ReportPriceDetailsViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/8/14.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "ReportPriceDetailsViewController.h"
#import "ReportPriceView.h"
#import "ReportPriceView1.h"
#import "ReportPriceContentView.h"
#import "QueryQuoteModel.h"

@interface ReportPriceDetailsViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)NSArray *style;
@property (nonatomic, strong)NSArray *titleArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) QueryQuoteModel *model;

@end

@implementation ReportPriceDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView:@"报价单详情"];
    self.tableView.backgroundColor = [UIColor whiteColor];
//    self.style = @[@[@"名称名称名称名称名称",@"100/蓝色",@"50"],@[@"名称名称名称名称名称",@"100/蓝色",@"50"],@[@"名称名称名称名称名称",@"100/蓝色",@"50"],@[@"名称名称名称名称名称",@"100/蓝色",@"50"],@[@"名称名称名称名称名称",@"100/蓝色",@"50"]];
    self.titleArray = @[@[@"产品名称",@"数量(件)",@"价格(元/件)"],@[@"面/里料名称",@"规格",@"价格"],@[@"辅料名称",@"规格",@"价格"]];
    [self requestData];
}

- (void)didReceiveMeoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestData {
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken, @"quoteId":self.quoteId==nil?@"":self.quoteId};
    [RequestData queryQuoteDetailParameters:[UIUtil toJsonString:dic] withBlock:^(QueryQuoteModel *model, NSString *msg, NSError *error) {
        if (error) {
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
        } else {
            self.model = model;
            [self.tableView reloadData];
        }
    }];
}

- (void)setModel:(QueryQuoteModel *)model {
    _model = model;
    self.style = @[@[model.name,model.minOrderNumber,model.quotation],@[model.name, model.optOrderNumber,model.quotation2]];
}

- (void) backToPrevious {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(12, 8, SCREEN_WIDTH - 24, (self.style.count + 1) * 30 - self.style.count - 1)];
        view.layer.masksToBounds = YES;
        view.layer.cornerRadius = 6.0;
        view.layer.borderWidth = 1;
        view.layer.borderColor = [[UIUtil colorWithHexString:@"#bdbaba"] CGColor];
        for (int i = 0; i < self.style.count + 1; i++) {
            if (i == 0) {
                ReportPriceView *countView = [[ReportPriceView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, 30)];
                countView.array = [self.titleArray objectAtIndex:indexPath.row];
                [view addSubview:countView];
            } else  {
                ReportPriceView *countView = [[ReportPriceView alloc] initWithFrame:CGRectMake(0, 30 * i-i-1, SCREEN_WIDTH - 24, 30)];
                countView.array = [self.style objectAtIndex:i-1];
                countView.layer.borderWidth = 1;
                countView.layer.borderColor = [[UIUtil colorWithHexString:@"#bdbaba"] CGColor];
                [view addSubview:countView];
            }
        }
        [cell addSubview:view];
    } else if (indexPath.row == 1) {
        ReportPriceContentView *view = [[ReportPriceContentView alloc] initWithFrame:CGRectMake(12, 8, SCREEN_WIDTH - 24, 100)];
        view.iffabric = YES;
        view.model = self.model;
        [cell addSubview:view];
    }  else if (indexPath.row == 2) {
        ReportPriceContentView *view = [[ReportPriceContentView alloc] initWithFrame:CGRectMake(12, 8, SCREEN_WIDTH - 24, 100)];
        view.iffabric = NO;
        view.model = self.model;
        [cell addSubview:view];
    } else {
        ReportPriceView1 *view = [[ReportPriceView1 alloc] initWithFrame:CGRectMake(12, 8,SCREEN_WIDTH-24, 324)];
        view.model = self.model;
        [cell addSubview:view];
    }
    return cell;
}


#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 3) {
        return 324;
    } else if (indexPath.row == 0) {
        return (self.style.count + 1)*30 - self.style.count - 1 + 8;
    } else {
        return 108;
    }
}

@end
